<template>
  <Card style="width:850px;margin: auto;">
    <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="150" style="width: 300px;margin: auto;">


      <FormItem label="原材料规格代码:" prop="code">
        <Input v-model="formLeft.code" :maxlength="2" disabled></Input>
      </FormItem>

      <FormItem label="原材料规格名称:" prop="name">
        <Input v-model="formLeft.name" :maxlength="10"></Input>
      </FormItem>

      <FormItem label="描述:" prop="describe">
        <Input v-model="formLeft.describe" type="textarea"></Input>
      </FormItem>


      <FormItem label="使用状态" prop="state">
        <Select v-model="formLeft.state">
          <Option v-for="item in stateList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>

      <row type="flex" justify="center">
        <i-col span="20">

        </i-col>
        <i-col span="30">
          <Button type="primary" @click="SpecificationsUpdateByCode">修改</Button>
          <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
        </i-col>
      </row>

    </Form>
  </Card>
</template>

<script>
  import {SpecificationsFindByCode,SpecificationsUpdateByCode} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 10,//pageSize:每页几条数据
        total: 0,
        stateList: [
          {
            value: '0',
            label: '不使用'
          }, {
            value: '1',
            label: '使用'
          }
        ],
        formLeft: {
          code: '',
          name: '',
          describe:'',
          state:'',
          makingsState:'原材料',
        },
        validateLibForm: {
          code: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          state: [
            {required: true, message: '请选择', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      const codeFlag = this.$route.query.code;
      if(codeFlag){
        this.formLeft.code = codeFlag;
        this.SpecificationsFindByCode();
      }
    },
    methods: {
      SpecificationsUpdateByCode() {
          if (this.formLeft.code !== null && this.formLeft.code !== "") {
            if (this.formLeft.code.length ==2){
            if (this.formLeft.name !== null && this.formLeft.name !== "") {
                SpecificationsUpdateByCode(this.formLeft).then(res => {
                  if (res.data.code==0){
                    this.$Message.success("编辑成功");
                    this.$router.go(-1);
                  } else {
                    this.$Message.error( res.data.msg);
                  }
                })
            } else {
              this.$Message.error("原材料规格名称不能为空");
            }
            } else {
              this.$Message.error("原材料规格代码格式不满足需求");
            }
          } else {
            this.$Message.error("原材料规格代码不能为空");
          }
      },
      back(){
        this.$router.go(-1);
      },
      SpecificationsFindByCode(){
        var data =  {
          code:this.formLeft.code,
          makingsState:this.formLeft.makingsState
        }
        SpecificationsFindByCode(data).then(res => {
          this.formLeft = res.data.data;
        })
      }
    },

  }
</script>


